﻿using UnityEngine;
using System.Collections;

public class ChapterChangeManager : MonoBehaviour {

	public CameraManager camManager;
	public CameraDisplay camDisplay;

	public bool isShifted;
	public bool isQuote;

	void Awake()
	{

	}
	void Start()
	{
		if(isQuote)
			StartCoroutine ("shiftQuote");
	}

	void Update()
	{
		if(!isShifted)
		{
			if(Input.GetKeyDown(KeyCode.Space))
			{
				StartCoroutine("shiftToMain");
				this.enabled = false;
			}
		}
		else
		{
			if(Input.GetKeyDown(KeyCode.Space))
			{
				StopCoroutine("shiftQuote");
				StartCoroutine("shiftQuote");
			}
		}
	}

	IEnumerator shiftToMain()
	{
		isShifted = false;
		yield return new WaitForSeconds (2);
		camManager.changeCamera (0);
		isShifted = true;
	}

	IEnumerator shiftQuote()
	{
		for(int i=0; i < camDisplay.sceneCameras.Length; i++)
		{
			yield return new WaitForSeconds(4);
			Camera.main.transform.Translate(Vector3.right * 20);
			/*camManager.changeCamera(i);*/ //hurr durr how does this line work?
			if(i == camDisplay.sceneCameras.Length - 1)
			{
				yield return new WaitForSeconds(3);
				LoadIntro();
			}
		}
	}

	void LoadIntro()
	{
		Application.LoadLevel("Introduction");
	}

	void OnLevelWasLoaded(int level)
	{
		isShifted = false;
		switch(level)
		{
			case 1:
			camManager = GameObject.Find ("CamManagerHolder").GetComponent<CameraManager> ();
			camDisplay = FindObjectOfType<CameraDisplay> ();
			isQuote = true;
			break;
			case 3:
			this.enabled = !this.enabled;
			break;
			case 4:
			camManager = GameObject.Find ("CamManagerHolder").GetComponent<CameraManager> ();
			camDisplay = FindObjectOfType<CameraDisplay> ();
			isQuote = false;
			this.enabled = true;
			break;
			case 5:
			camManager = GameObject.Find ("CamManagerHolder").GetComponent<CameraManager> ();
			camDisplay = FindObjectOfType<CameraDisplay> ();
			isQuote = false;
			this.enabled = true;
			break;
			case 6:
			camManager = GameObject.Find ("CamManagerHolder").GetComponent<CameraManager> ();
			camDisplay = FindObjectOfType<CameraDisplay> ();
			isQuote = false;
			this.enabled = true;
			break;
			case 7:
			camManager = GameObject.Find ("CamManagerHolder").GetComponent<CameraManager> ();
			camDisplay = FindObjectOfType<CameraDisplay> ();
			isQuote = false;
			this.enabled = true;
			break;
		}
	}
}
